package weilus.clients.dtos;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import java.util.concurrent.TimeoutException;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MQMessage {
    public enum Type {
        TIME_OUT, UNKNOWN_SERVICE, BUSINESS_ERROR, UNKNOWN_API,CIRCUIT_BREAKER, UNAUTH,NONE
    }
    private String name;
    private String age;
    private Long delay;
    private Boolean ex;
    private Type type = Type.NONE;
    private String routingKey;

    public void setType(Throwable ex){
        if(ex instanceof WebClientResponseException){
            switch (((WebClientResponseException) ex).getRawStatusCode()){
                case 500:
                    this.type = Type.BUSINESS_ERROR;
                    break;
                case 503:
                    this.type = Type.UNKNOWN_SERVICE;
                    break;
                case 404:
                    this.type = Type.UNKNOWN_API;
                    break;
                case 403:
                    this.type = Type.UNAUTH;
                    break;
            }
        } else if(ex instanceof TimeoutException){
            this.type = Type.TIME_OUT;
        } else if(ex instanceof RuntimeException){
            this.type = Type.CIRCUIT_BREAKER;
        }else {
            throw new RuntimeException("未知的服务降级.........",ex);
        }
    }
}
